package apiRedis;

import com.techindata.cacheredis.RedisConnectionPool;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisShardInfo;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * User: huangpu@tsinghuabigdata.com
 * Date: 2016/2/23 13:55
 * Description: redis中的hash操作.
 */
public class DynamicCacheHash {
    static Logger logger = Logger .getLogger(DynamicCacheHash.class);

    /**
     * 获取key值下所有的域-值
     * @param key 传入的key值
     * @return Map<String,String> 类型
     */
    public static Map<String, String> hgetAll(String servIp,String servPort,String passwd ,String key){
        JedisPool pool = DynamicRedisPoolUtil.getJedisPool(servIp,servPort,passwd);
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.hgetAll(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 获取key值下所有的域
     * @param key 传入的key值
     * @return Set<String> 类型
     */
    public static Set<String> hkeys(String servIp,String servPort,String passwd ,String key){
        JedisPool pool = DynamicRedisPoolUtil.getJedisPool(servIp,servPort,passwd);
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.hkeys(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 批量获取key值下指定域的数据
     * @param key 传入的key值
     * @param fields 需要查询的域
     * @return List<String> 类型
     */
    public static List<String> hmget(String servIp,String servPort,String passwd ,String key,String... fields){
        JedisPool pool = DynamicRedisPoolUtil.getJedisPool(servIp,servPort,passwd);
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.hmget(key, fields);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 获取key值下指定域的数据
     * @param key 传入的key值
     * @param field 需要查询的域
     * @return String 类型
     */
    public static String hget(String servIp,String servPort,String passwd ,String key,String field){
        JedisPool pool = DynamicRedisPoolUtil.getJedisPool(servIp,servPort,passwd);
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.hget(key, field);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 删除key值下指定域的数据
     * @param key 传入的key值
     * @param field 需要查询的域
     * @return String 类型
     */
    public static long hdel(String servIp,String servPort,String passwd ,String key,String field) throws Exception {
        JedisPool pool = DynamicRedisPoolUtil.getJedisPool(servIp,servPort,passwd);
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.hdel(key, field);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1L;
        } finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 删除key值下指定域的数据
     * @param key 传入的key值
     * @param field 需要查询的域
     * @return String 类型
     */
    public static boolean hexists(String servIp,String servPort,String passwd ,String key,String field) throws Exception {
        JedisPool pool = DynamicRedisPoolUtil.getJedisPool(servIp,servPort,passwd);
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.hexists(key, field);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return false;
        } finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 批量保存key的数据
     * @param key 传入的key值
     * @param hash 需要保存域-值
     * @return String 类型
     */
    public static String hmset(String servIp,String servPort,String passwd ,String key,Map<String, String> hash){
        JedisPool pool = DynamicRedisPoolUtil.getJedisPool(servIp,servPort,passwd);
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.hmset(key, hash);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 保存key的数据
     * @param key 传入的key值
     * @param field 域
     * @param value 值
     * @return String 类型
     */
    public static Long hset(String servIp,String servPort,String passwd ,String key,String field, String value){
        JedisPool pool = DynamicRedisPoolUtil.getJedisPool(servIp,servPort,passwd);
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.hset(key, field,value);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return 0L;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 为哈希表 key 中的指定字段的整数值加上增量 increment 
     * @param key 传入的key值
     * @param field 域
     * @param value 值
     * @return String 类型
     */
    public static Long hincrBySet(String servIp,String servPort,String passwd ,String key,String field, Long value){
    	JedisPool pool = DynamicRedisPoolUtil.getJedisPool(servIp,servPort,passwd);
    	Jedis jedis = null;
    	try {
    		jedis = pool.getResource();
    		return jedis.hincrBy(key, field,value);
    	} catch (Exception e) {
    		logger.error(e.getMessage(), e);
    		return 0L;
    	}
    	finally{
    		RedisConnectionPool.returnResource(pool, jedis);
    	}
    }
}
